package com.ruoyi.finance.protal.utils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class UUID {

    // 用户邀请码生成规则
    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int CODE_LENGTH = 8;

    /**
     * 生成一个 邀请码
     * @return
     */
    public static String getInvitationCode() {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(CODE_LENGTH);
        for (int i = 0; i < CODE_LENGTH; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        return sb.toString();
    }

    /**
     * 获取一个uuid
     * @return
     */
    public static String uuid(){
        return java.util.UUID.randomUUID().toString().replace("-","");
    }

    /**
     * 随机生成验证码
     * @param length 长度为4位或者6位
     * @return
     */
    public static String generateValidateCode(int length){
        Integer code =null;
        if(length == 4){
            code = new SecureRandom().nextInt(9999);//生成随机数，最大为9999
            if(code < 1000){
                code = code + 1000;//保证随机数为4位数字
            }
        }else if(length == 6){
            code = new SecureRandom().nextInt(999999);//生成随机数，最大为999999
            if(code < 100000){
                code = code + 100000;//保证随机数为6位数字
            }
        }else{
            throw new RuntimeException("只能生成4位或6位数字验证码");
        }
        return code.toString();
    }


    /**
     * 生成订单号
     * @return
     */
    public static String getCustomerOrderNo() {
        SecureRandom rand = new SecureRandom();
        int num=rand.nextInt(100)+1;
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+num;
    }

}
